package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@Service
public class AddressBookServiceImpI implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;
    /**
     * 查询当前登录用户的所有地址信息
     * @return
     */
    @Override
    public List<AddressBook> queryAllAddressBook() {

        return addressBookMapper.queryAllAddressBook();

    }
    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @Override
    public void addAddressBook(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressBookMapper.addAddressBook(addressBook);
    }
    /**
     * 设置默认地址
     *
     * @param addressBook
     */
    @Override
    public void setDefaultAddressBook(AddressBook addressBook) {
        //1、将当前用户的所有地址修改为非默认地址 update address_book set is_default = ? where user_id = ?
        addressBook.setIsDefault(0);
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.updateIsDefaultByUserId(addressBook);

        //2、将当前地址改为默认地址 update address_book set is_default = ? where id = ?
        addressBook.setIsDefault(1);
        addressBookMapper.update(addressBook);
    }





    /**
     * 根据id修改地址
     *
     * @param addressBook
     */
    public void update(AddressBook addressBook) {
        addressBookMapper.update(addressBook);
    }

    /**
     * 根据id查询地址
     * @return
     */
    @Override
    public AddressBook queryAddressBookById(Long id) {
        AddressBook addressBook=addressBookMapper.queryAddressBookById(id);
        return addressBook;
    }

    /**
     * 根据Id删除地址
     * @return
     */
    @Override
    public void deleteAddressBookById(Long id) {

        addressBookMapper.deleteAddressBookById(id);
    }
}
